package SGEngine

import (
	"fmt"
	gl "github.com/gl"
	"github.com/glfw"

	//glu "github.com/glu"
)

var WinRoot *Window

type Window struct {
	MainScene *Scene
	Width     int
	Height    int
	Title     string
}

func CreateWindow(width int, height int, title string) *Window {
	RWindow := new(Window)
	WinRoot = RWindow
	RWindow.Width = width
	RWindow.Height = height
	RWindow.Title = title
	RWindow.Init()
	RWindow.MainScene = CreateScene()
	//LoadFont("source/chinese.txt")

	glfw.SetMouseButtonCallback(OnMouseClick)
	glfw.SetWindowSizeCallback(OnWinReSize)
	glfw.SetMousePosCallback(OnMouseMove)

	return RWindow
}

func (w *Window) Init() {
	if err := glfw.Init(); err != nil {
		fmt.Println("glfw init err")
		return
	}
	glfw.OpenWindowHint(glfw.WindowNoResize, 1)
	if err := glfw.OpenWindow(w.Width, w.Height, 0, 0, 0, 0, 16, 0, glfw.Windowed); err != nil {
		fmt.Println("glfw openwindow err")
		return
	}
	glfw.SetSwapInterval(1)
	glfw.SetWindowTitle(w.Title)
	gl.Init()

}

func (w *Window) Quit() {
	glfw.Terminate()
	glfw.CloseWindow()
}

func (w *Window) Run() {
	gl.MatrixMode(gl.MODELVIEW)

	for glfw.WindowParam(glfw.Opened) == 1 {
		ACTimeUpdate.update()
		w.MainScene.SceneLoop()
		glfw.SwapBuffers()
	}
	w.Quit()

}
